<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<script type="text/javascript">
  var resourceTree;
  $(function() {
    var newOptions = {
      parentField : 'preId',
      multiple: true,
      checkbox : true,
      panelHeight : 'auto',
      onLoadSuccess : function() {
        var ids = $.stringToList('${role.resourceIds}');
        if (ids.length > 0) {
          for ( var i = 0; i < ids.length; i++) {
            if ( $('#resourceTree').tree('find', ids[i])) {
              $('#resourceTree').tree('check', resourceTree.tree('find', ids[i]).target);
            }
          }
        }
        $('#roleGrantLayout').layout('panel', 'west').panel('setTitle', $.formatString('[{0}]角色可以访问的资源', '${role.name}'));
        parent.$.messager.progress('close');
      },
      cascadeCheck : true,
      postData:{"id":$('#roleIdFrom').text()}
    }
    resourceTree = initTree('/roleController/rolesTree.html', $('#resourceTree'), newOptions);
    $('#form').form({
      url : '${pageContext.request.contextPath}/roleController/addRole.html',
      onSubmit : function() {
        parent.$.messager.progress({
          title : '提示',
          text : '数据处理中，请稍后....'
        });
        var isValid = $(this).form('validate');
        if (!isValid) {
          parent.$.messager.progress('close');
        }
        var nodes1 = $('#resourceTree').tree('getChecked');
        var nodes2 = $('#resourceTree').tree('getChecked', 'indeterminate');
        var checknodes = $.merge(nodes1,nodes2);
        var ids = [];
        if (checknodes && checknodes.length > 0) {
          for ( var i = 0; i < checknodes.length; i++) {
            ids.push(checknodes[i].id);
          }
        }
        $('#resourceIds').val(ids);
        return isValid;
      },
      success : function(result) {
        parent.$.messager.progress('close');
        result = $.parseJSON(result);
        if (result && isSuccess(result.result)) {
          parent.$.modalDialog.openner_treeGrid.datagrid('getPager').pagination('select');//之所以能在这里调用到parent.$.modalDialog.openner_treeGrid这个对象，是因为role.jsp页面预定义好了
          parent.$.modalDialog.handler.dialog('close');
        }
      }
    });
//
//    function checkAll() {
//      var nodes = resourceTree.tree('getChecked', 'unchecked');
//      if (nodes && nodes.length > 0) {
//        for ( var i = 0; i < nodes.length; i++) {
//          resourceTree.tree('check', nodes[i].target);
//        }
//      }
//    }
//    function uncheckAll() {
//      var nodes = resourceTree.tree('getChecked');
//      if (nodes && nodes.length > 0) {
//        for ( var i = 0; i < nodes.length; i++) {
//          resourceTree.tree('uncheck', nodes[i].target);
//        }
//      }
//    }
//    function checkInverse() {
//      var unchecknodes = resourceTree.tree('getChecked', 'unchecked');
//      var checknodes = resourceTree.tree('getChecked');
//      if (unchecknodes && unchecknodes.length > 0) {
//        for ( var i = 0; i < unchecknodes.length; i++) {
//          resourceTree.tree('check', unchecknodes[i].target);
//        }
//      }
//      if (checknodes && checknodes.length > 0) {
//        for ( var i = 0; i < checknodes.length; i++) {
//          resourceTree.tree('uncheck', checknodes[i].target);
//        }
//      }
//    }
  });

</script>
<var id="roleIdFrom" style="display: none">${roleId}</var>
<div id="roleGrantLayout" class="easyui-layout" data-options="fit:true,border:false">
  <div data-options="region:'center'" title="系统资源" style="width: 300px; padding: 1px;">
    <div class="well well-small">
      <form id="form" method="post">
        <input name="id" type="hidden" class="span2" value="${roleId}" readonly="readonly">
        <ul id="resourceTree"></ul>
        <input id="resourceIds" name="choiceId" type="hidden"  />
      </form>
    </div>
  </div>
  <%--<div data-options="region:'center'" title="" style="overflow: hidden; padding: 10px;">--%>
    <%--&lt;%&ndash;<div class="well well-small">&ndash;%&gt;--%>
      <%--&lt;%&ndash;<span class="label label-success">${role.name}</span>&ndash;%&gt;--%>
      <%--&lt;%&ndash;<div>${role.remark}</div>&ndash;%&gt;--%>
    <%--&lt;%&ndash;</div>&ndash;%&gt;--%>
    <%--<div class="well well-large">--%>
      <%--<button class="btn btn-success" onclick="checkAll();">全选</button>--%>
      <%--<br /> <br />--%>
      <%--<button class="btn btn-warning" onclick="checkInverse();">反选</button>--%>
      <%--<br /> <br />--%>
      <%--<button class="btn btn-inverse" onclick="uncheckAll();">取消</button>--%>
    <%--</div>--%>
  <%--</div>--%>
</div>